package it.cagiobox.fileutility.bo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class Prop {
	private Integer _originalFileLineNumber;
	private String _key;
	private List<String> _values;
	private Section _parentSection;
	static final String KEYVALUES_SEPARATOR = "=";
	static final String VALUES_SEPARATOR = ",";

	/**
	 * @param str example: mykey=myvalue1,myvalue2
	 * @param lineNumber
	 */
	public Prop(String str, Integer lineNumber, Section parentSection) {

		if (str.indexOf("=") == -1) {
			throw new IllegalArgumentException(
					"Dichiarazione di proprietà non conforme. Manca '"
							+ KEYVALUES_SEPARATOR + "':" + str + "  Riga:"
							+ lineNumber);
		}

		_originalFileLineNumber = lineNumber;
		String[] keyLevel = str.split(KEYVALUES_SEPARATOR);
		_key = keyLevel[0];
		_values = new ArrayList<String>( Arrays.asList(keyLevel[1].split(VALUES_SEPARATOR)));
		_parentSection = parentSection;  //the section owning this property
	}

	public void sort() {
		Collections.sort(_values);
	}

	public String getKey() {
		return _key;
	}

	public Integer getOriginalFileRowNumber() {
		return _originalFileLineNumber;
	}

	public List<String> getValues() {
		return _values;
	}

	public String getValuesAsString() {
		String values = "";

		for (String val : _values) {
			values += "," + val;
		}
		return values;
	}

	public Section getParentSection() {
		return _parentSection;
	}


}

// Comparator
class AlphabeticDescendingPropComparator implements Comparator<Prop> {
	public int compare(Prop a, Prop b) {

		int sdif = a.getKey().compareToIgnoreCase(b.getKey());

		return sdif;
	}
}
